package com.retail.product.controller;

import com.retail.common.core.domain.Result;
import com.retail.common.core.utils.poi.ExcelUtil;
import com.retail.common.core.web.controller.BaseController;
import com.retail.common.core.web.page.TableDataInfo;
import com.retail.common.log.annotation.Log;
import com.retail.common.log.enums.BusinessType;
import com.retail.common.security.annotation.RequiresPermissions;

import com.retail.product.common.domain.TBrand;
import com.retail.product.service.ITBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 品牌Controller
 * 
 * @author retail
 * @date 2024-02-26
 */
@RestController
@RequestMapping("/brand")
public class TBrandController extends BaseController
{
    @Autowired
    private ITBrandService tBrandService;

    /**
     * 查询品牌列表
     */
    @RequiresPermissions("product:brand:list")
    @GetMapping("/list")
    public TableDataInfo list(TBrand tBrand)
    {
        startPage();
        List<TBrand> list = tBrandService.selectTBrandList(tBrand);
        return getDataTable(list);
    }

    /**
     * 导出品牌列表
     */
    @RequiresPermissions("product:brand:export")
    @Log(title = "品牌", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TBrand tBrand)
    {
        List<TBrand> list = tBrandService.selectTBrandList(tBrand);
        ExcelUtil<TBrand> util = new ExcelUtil<TBrand>(TBrand.class);
        util.exportExcel(response, list, "品牌数据");
    }

    /**
     * 获取品牌详细信息
     */
    @RequiresPermissions("product:brand:query")
    @GetMapping(value = "/{brandId}")
    public Result getInfo(@PathVariable("brandId") Integer brandId)
    {
        return success(tBrandService.selectTBrandByBrandId(brandId));
    }

    /**
     * 新增品牌
     */
    @RequiresPermissions("product:brand:add")
    @Log(title = "品牌", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody TBrand tBrand)
    {
        return toAjax(tBrandService.insertTBrand(tBrand));
    }

    /**
     * 修改品牌
     */
    @RequiresPermissions("product:brand:edit")
    @Log(title = "品牌", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody TBrand tBrand)
    {
        return toAjax(tBrandService.updateTBrand(tBrand));
    }

    /**
     * 删除品牌
     */
    @RequiresPermissions("product:brand:remove")
    @Log(title = "品牌", businessType = BusinessType.DELETE)
	@DeleteMapping("/{brandIds}")
    public Result remove(@PathVariable Integer[] brandIds)
    {
        return toAjax(tBrandService.deleteTBrandByBrandIds(brandIds));
    }

    @RequiresPermissions("product:brand:import")
    @Log(title = "品牌",businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public Result importData(MultipartFile file) throws IOException {
        ExcelUtil<TBrand> excelUtil = new ExcelUtil<> (TBrand.class);
        List<TBrand> brandList = excelUtil.importExcel (file.getInputStream ());
        String message = tBrandService.importBrand(brandList);
        return success (message);
    }
}
